Ext.define('CA.dictionary.LineData', {
    extend: 'CA.base.Grid',
    xtype: 'CA.dictionary.LineData',

    initComponent: function(){
        var fields = ['id'],
            columns = [{
                text: '№',
                tdCls: 'x-grid-cell-special',
                dataIndex: 'id',
                width: 40
            }],
            code = this.dictionary.data.code;

        Ext.each(this.dictionary.meta, function(field){
            var data = this.createColumn(field);

            fields.push(data.field);
            columns.push(data.column);
        }, this);

        Ext.apply(this, {
            columns: columns,
            controller: 'LineDictionary',
            extraParams: { dic: code },
            layout: 'fit',
            border: false,
            removable: true,
            autoScroll: true,
            modelFields: fields,
            modelIdProperty: 'id',
            buildToolbar: function(){ return []; }
        });

        this.callParent();
    },

    createRecord: function(){
        var code = this.dictionary.data.code,
            el = this.getEl();

        el.mask();

        LineDictionary.create(code, function(resp){
            el.unmask();
            this.reload();
        }, this);
    },

    removeSelected: function(){
        var recs = this.getSelectionModel().selected.items;

        if(recs.length){
            this.doRemoveRecord(recs);
        }
    },

    createColumn: function(data){
        var editor = { xtype: 'textfield' },
            column = { text: data.code, dataIndex: data.code, editor: editor },
            field = { name: data.code };

        switch(data.type){
            case 'boolean':
                editor.xtype = 'checkbox';
                editor.fieldStyle = 'margin-left:5px';
                column.width = 30;
                column.xtype = 'booleancolumn';
                column.trueText = CA.trueText;
                column.falseText = CA.falseText;
                break;

            default:
                column.flex = 1;
                break;
        }

        return {
            column: column,
            field: field
        };
    }
});